package io.github.stary1993.kite.auth.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import io.github.stary1993.kite.auth.entity.SysRole;
import io.github.stary1993.kite.auth.entity.SysUser;
import io.github.stary1993.kite.auth.mapper.SysUserMapper;
import io.github.stary1993.kite.auth.mapper.SysUserRoleMapper;
import io.github.stary1993.kite.auth.service.SysRoleService;
import io.github.stary1993.kite.auth.service.SysUserService;
import io.github.stary1993.kite.auth.vo.UserVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author stary1993
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends MPJBaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();


    private final SysRoleService sysRoleService;


    private final SysUserRoleMapper sysUserRoleMapper;


    /**
     * @param userId
     * @return
     */
    @Override
    public UserVO detailsById(Long userId) {
        SysUser sysUser = this.getById(userId);
        UserVO userVO = BeanUtil.copyProperties(sysUser, UserVO.class);
        List<SysRole> roleList = sysRoleService.findRolesByUserId(userId);
        userVO.setRoleList(roleList);
        return userVO;
    }

    /**
     * @param username
     * @return
     */
    @Override
    public UserVO detailsByUsername(String username) {
        SysUser sysUser = this.lambdaQuery().eq(SysUser::getUsername, username).one();
        UserVO userVO = BeanUtil.copyProperties(sysUser, UserVO.class);
        List<SysRole> roleList = sysRoleService.findRolesByUserId(sysUser.getUserId());
        userVO.setRoleList(roleList);
        return userVO;
    }

    @Override
    public UserVO detailsByPhone(String phone) {
        SysUser sysUser = this.lambdaQuery().eq(SysUser::getPhone, phone).one();
        UserVO userVO = BeanUtil.copyProperties(sysUser, UserVO.class);
        List<SysRole> roleList = sysRoleService.findRolesByUserId(sysUser.getUserId());
        userVO.setRoleList(roleList);
        return userVO;
    }
}
